const { Controller } = require("egg");

class EnterpriseCertificateController extends Controller {

    async list() {
        const { ctx, service } = this;
        const { pageNum = 1, pageSize = 20, search } = ctx.query;

        try {
            const result = await service.enterpriseCertificate.findAll({
                pageNum: parseInt(pageNum),
                pageSize: parseInt(pageSize),
                search
            });
            this.ctx.helper.success(result);
        } catch (error) {
            this.ctx.helper.error(error);
        }
    }

    async create() {
        const { ctx, service } = this;
        const {
            enterpriseName,
            certificateTypeName,
            nationalLevel,
            certificateNumber,
            annualInspectionYears,
            validityStartDate,
            validityEndDate,
            remarks
        } = ctx.request.body;

        try {
            const createdCertificate = await service.enterpriseCertificate.create({
                enterpriseName,
                certificateTypeName,
                nationalLevel,
                certificateNumber,
                annualInspectionYears,
                validityStartDate,
                validityEndDate,
                remarks,
                isDeleted: 0
            });
            this.ctx.helper.success(createdCertificate);
        } catch (error) {
            this.ctx.helper.error(error);
        }
    }

    async createOrUpdate() {
        const { ctx, service } = this;
        const {
            enterpriseName,
            certificateTypeName,
            nationalLevel,
            certificateNumber,
            annualInspectionYears,
            validityStartDate,
            validityEndDate,
            remarks
        } = ctx.request.body;

        try {
            const createdCertificate = await service.enterpriseCertificate.createOrUpdate({
                enterpriseName,
                certificateTypeName,
                nationalLevel,
                certificateNumber,
                annualInspectionYears,
                validityStartDate,
                validityEndDate,
                remarks,
                isDeleted: 0
            });
            this.ctx.helper.success(createdCertificate);
        } catch (error) {
            this.ctx.helper.error(error);
        }
    }


    async delete() {
        const { ctx, service } = this;
        const { id } = ctx.request.body;
        try {
            const deletedCount = await service.enterpriseCertificate.delete(id);
            if (deletedCount) {
                this.ctx.helper.success({ message: "删除成功" });
            } else {
                this.ctx.helper.error("记录不存在或已被删除");
            }
        } catch (error) {
            this.ctx.helper.error(error);
        }
    }

    async update() {
        const { ctx, service } = this;
        const { id, ...updates } = ctx.request.body;

        try {
            const [affectedRows] = await service.enterpriseCertificate.update(id, updates);
            if (affectedRows) {
                this.ctx.helper.success({ message: "更新成功" });
            } else {
                this.ctx.helper.error("记录不存在或更新失败");
            }
        } catch (error) {
            this.ctx.helper.error(error);
        }
    }

    async updateAnnualInspectionYears() {
        const { ctx, service } = this;
        const { id, time } = ctx.request.body;
        const date = new Date(time);

        try {
            const updated = await service.enterpriseCertificate.updateAnnualInspectionYears(id, date);
            if (updated) {
                this.ctx.helper.success(updated);
            } else {
                this.ctx.helper.error("年检年限更新失败");
            }
        } catch (error) {
            this.ctx.helper.error(error);
        }
    }
}

module.exports = EnterpriseCertificateController;